package com.snails.app.utils;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.snails.app.SnailsApp;
import com.snails.tool.storage.SharedPrefUtils;

import java.io.Serializable;
import java.util.Set;

public class StorageSingleton {
    private static volatile StorageSingleton instance = null;
    private SharedPrefUtils sharedPrefUtils = null;

    public static StorageSingleton instance() {
        if (instance == null) {
            synchronized (StorageSingleton.class) {
                if (instance == null)
                    instance = new StorageSingleton();
            }
        }
        return instance;
    }

    private StorageSingleton() {
        final Context ctx = SnailsApp.instance().getCtx();
        if (ctx == null) {
            throw new NullPointerException("请先调用【SnailsApp.instance().init()】进行初始化。");
        }
        sharedPrefUtils = new SharedPrefUtils(ctx, "app-storage");
    }

    public StorageSingleton putString(String name, String value) {
        sharedPrefUtils.putString(name, value);
        return this;
    }

    public String getString(String name, String defaultValue) {
        return sharedPrefUtils.getString(name, defaultValue);
    }


    public StorageSingleton putInt(String name, int value) {
        sharedPrefUtils.putInt(name, value);
        return this;
    }

    public int getInt(String name, int defaultValue) {
        return sharedPrefUtils.getInt(name, defaultValue);
    }


    public StorageSingleton putFloat(String name, float value) {
        sharedPrefUtils.putFloat(name, value);
        return this;
    }

    public float getFloat(String name, float defaultValue) {
        return sharedPrefUtils.getFloat(name, defaultValue);
    }


    public StorageSingleton putLong(String name, long value) {
        sharedPrefUtils.putLong(name, value);
        return this;
    }

    public long getLong(String name, long defaultValue) {
        return sharedPrefUtils.getLong(name, defaultValue);
    }


    public StorageSingleton putBoolean(String name, boolean value) {
        sharedPrefUtils.putBoolean(name, value);
        return this;
    }

    public boolean getBoolean(String name, boolean defaultValue) {
        return sharedPrefUtils.getBoolean(name, defaultValue);
    }



    public <T extends Serializable> StorageSingleton putSerializable(String name, T obj) {
        if (obj != null) {
            String serializable = "";
            try {
                serializable = JSONObject.toJSONString(obj);
            } catch (Exception e) {
                e.printStackTrace();
                serializable = "";
            }
            if (!TextUtils.isEmpty(serializable)) putString(name, serializable);
        }
        return this;
    }

    @Nullable
    public <T extends Serializable> T getSerializable(String name, Class<T> clazz) {
        final String serializable = getString(name, "");
        if (!TextUtils.isEmpty(serializable)) {
            try {
                return JSONObject.parseObject(serializable, clazz);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public StorageSingleton putStringSet(String name, Set<String> value) {
        sharedPrefUtils.putStringSet(name, value);
        return this;
    }

    public Set<String> getStringSet(String name, Set<String> defaultValue) {
        return sharedPrefUtils.getStringSet(name, defaultValue);
    }


    public StorageSingleton removeCache(String name) {
        sharedPrefUtils.removeCache(name);
        return this;
    }

    public StorageSingleton clear() {
        sharedPrefUtils.clear();
        return this;
    }

    public void commit() {
        sharedPrefUtils.commit();
    }

}
